package com.xiaozhengkeji.xiaozhengblog.services.bus;

import com.alibaba.fastjson.JSON;
import com.xiaozhengkeji.xiaozhengblog.base.entitys.ArticleClassEntity;
import com.xiaozhengkeji.xiaozhengblog.base.entitys.SystemUserEntity;
import com.xiaozhengkeji.xiaozhengblog.base.service.ArticleClassEntityService;
import com.xiaozhengkeji.xiaozhengblog.base.service.SystemUserEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("ArticleUtilsServiceBus")
public class ArticleUtilsServiceBus {
    ArticleClassEntityService articleClassEntityService;
    SystemUserEntityService systemUserEntityService;

    @Autowired
    public ArticleUtilsServiceBus(ArticleClassEntityService articleClassEntityService, SystemUserEntityService systemUserEntityService) {
        this.articleClassEntityService = articleClassEntityService;
        this.systemUserEntityService = systemUserEntityService;
    }

    /*判断此用户是否有此分类文章的权限*/
    public Boolean isUserHasArticleClassAccess(String articleClass, String userName) {
        List<ArticleClassEntity> articleClassEntity = articleClassEntityService.queryAllByArticleClass(articleClass);
        if (articleClassEntity.size() > 0) {
            //文章权限是默认权限
            if (articleClassEntity.get(0).getArticleClassPurview().equals("user:def")) {
                return true;
            }
            if (!userName.equals("tourist")) {
                List<SystemUserEntity> systemUserEntity = systemUserEntityService.queryByUsername(userName);
                if (systemUserEntity.size() > 0) {
                    for (String permission : JSON.parseArray(systemUserEntity.get(0).getPermission(), String.class)) {
                        if (permission.equals(articleClassEntity.get(0).getArticleClassPurview())) {
                            return true;
                        }
                    }
                    return false;
                }
                return false;
            }
            return false;
        }
        return false;
    }
}
